import subprocess
import sys
import shutil
import os
import time

def rm_file():
    folder_path = r"D:\Gitee\temperature_-monitoring\allure-results" 
    if os.path.exists(folder_path):
        try:
            shutil.rmtree(folder_path)
            print(f"文件夹 {folder_path} 已删除")
        except OSError as e:
            print(f"删除失败: {e.strerror}")
    else:
        print(f"文件夹 {folder_path} 不存在，跳过")
def run_pytest():
    # 构建 pytest 命令
    command = [sys.executable, "-m", "pytest","-vs","--alluredir=./allure-results","testcases/test_cookie_login.py","testcases/test_home.py","testcases/test_Statistics.py",
               "testcases/test_monitor.py","testcases/test_history.py","testcases/test_alarm.py","testcases/test_runreport.py","testcases/test_userlist.py"]
    # command = [sys.executable, "-m", "pytest","-vs","--alluredir=./allure-results","testcases/test_login.py","testcases/test_home.py","testcases/test_Statistics.py","testcases/test_monitor.py","testcases/test_history.py"]  
    # command = [sys.executable, "-m", "pytest","-vs","testcases/test_cookie_login.py","testcases/test_Statistics.py"]
    # command = [sys.executable, "-m", "pytest","-vs","--alluredir=./allure-results","testcases/test_cookie_login.py","testcases/test_userlist.py"]
    # 使用 subprocess 运行 pytest 命令
    result = subprocess.run(command, capture_output=True, text=True)
    # 输出 pytest 的执行结果
    print("=== 测试输出 ===")
    print(result.stdout)
    if result.stderr:
        print("\n=== 错误信息 ===")
        print(result.stderr)



def show_allure_simple():
    """使用 os.system 启动 Allure 报告"""
    results_dir = "./allure-results"
    
    # 检查目录是否存在
    if not os.path.exists(results_dir):
        print(f"❌ 错误：目录不存在 {results_dir}")
        return
    
    # 执行命令（会阻塞直到关闭浏览器）
    return_code = os.system(f"allure serve {results_dir}")
    
    
    if return_code == 0:
        print("✅ Allure 报告生成成功")
    else:
        print(f"❌ 失败，返回码: {return_code}")

# 调用

if __name__ == "__main__":
    rm_file()
    run_pytest()
    show_allure_simple()



